<?php
/* 
 *Dung de quyet he thong de lay ra cac file can translate
 */

/**
 * Description of ScanSystem
 *
 * @author zxcms
 */
class H_Translate_Render_ScanSystem {
    private $dirPath ;
    private $svnDir = array('/.','/..','/.svn');
    private $extention = array('phtml','php','js');
    private $patten = array(
        "/->t[(]['\\\"](.*?)['\\\"][)]/",
        "/CMS\.t[(]['\\\"](.*?)['\\\"][)]/"
    );
    public function __construct($dir){
            $this->dirPath = $dir;
    }
    /*
     * Return Array of DirPath
    */
    //Dua them file extention vao
    public function addExtention($extension){
        array_push($this->extention, $extension);
    }
    public function addPatten($patten){
        array_push($this->patten, $patten);
    }
    
    private function listInOneDir($dirPath){
        if (is_dir($dirPath)){
            $result = array();
            $dirList = scandir($dirPath);
            foreach ($dirList as $item){
                $dir = $dirPath ."/".$item;
                if (is_dir($dir) && !$this->isSvn($dir)){
                    array_push($result, $dir);
                    $child = $this->listInOneDir($dir);
                    foreach ($child as $itemChild){
                        $result[] = $itemChild;
                    }
                }
            }
            return $result;
        }else{
                return null;
        }
    }
    //Co the dirPath la mot Array
    private function listDir($dirPath = null){
            if ($dirPath == null){
                    $dirPath = $this->dirPath;
            }
            if (is_array($dirPath)){
                $result = array();
                foreach ($dirPath as $dir){
                    if ($this->listInOneDir($dir)!=null){
                        $result = array_merge($result,$this->listInOneDir($dir));
                    }
                    array_push($result, $dir);
                }
                if (count($result)>0) return $result;
                return null;
            }else{
                $result = $this->listInOneDir($dirPath);
                array_push($result,$dirPath);
                return $result;
            }
            
    }
    /*
     * Return File in Dir
     */
    private function listFileInDir($dirPath){
            if (is_dir($dirPath)){
                    $fileList = scandir($dirPath);
                    $result = array();
                    foreach ($fileList as $fileItem){
                            $file = $dirPath."/".$fileItem;
                            if (is_file($file)&&!$this->isSvn($file)&& $this->isExtension($file)){
                                    array_push($result, $file);
                            }
                    }
                    return $result;
            }else{
                    return null;
            }
    }
    private function isSvn($file){
            $match = null;
            preg_match("/.*(\/.*$)/i", $file,$match);
            foreach ($this->svnDir as $except){
                    if ($match[1] == $except){
                            return true;
                    }
            }
            return false;
    }
    private function isExtension($file){
            $extension = $this->getFileExtention($file);
            return (in_array($extension, $this->extention));
    }
    private function getFileExtention($file){
            if (is_file($file)){
                    $match = array();
                    preg_match("/.*\.(.*$)/i", $file,$match);
                    return $match[1];
            }else{
                    return null;
            }
    }

    public function getTranslateContent(){
            $allDir = $this->listDir($this->dirPath);            
            $result = array();
            if (count($allDir)>0){
                foreach ($allDir as $dir){
                    $listFile = $this->listFileInDir($dir);
                    if ($listFile != null){
                        foreach ($listFile as $file){
                                $tmp = $this->getTranslate($file);
                                foreach ($tmp as $key => $tmpItem){
                                        if (!in_array($tmpItem, $result)){
                                                $result[] = $tmpItem;
                                        }
                                }
                        }
                    }
                }
            }
            sort($result);
            $finalResult = array();
            foreach ($result as $key=>$item)
            {
                $item = stripslashes(H_S::html($item));
                $finalResult[$item] = null;
            }            
            return $finalResult;
    }

    private function getTranslate($file){
            $content = file_get_contents($file);
            if (is_file($file)){
                $result = array();                
                foreach ($this->patten as $patten){                    
                    preg_match_all($patten, $content,$match);                    
                    if (is_array($match[1])){
                        $result = array_merge($result,$match[1]);
                    }
                }                
                if (count($result)>0) return $result;
                else return array();
            }else{
                return array();
            }
    }//put your code here
}
?>
